'use strict';
// 定期产品列表管理
app.controller('index', ['$scope', '$http', 'commonFactory', 'toaster', '$timeout', '$modal', '$interval', function ($scope, $http, commonFactory, toaster, $timeout, $modal, $interval) {
    var initdata = function () {
        $scope.godata = function () {
            requestProductList()
        }

        //下一页
        $scope.next = function () {
            if ($scope.page) {
                if ($scope.requestArr.pageIndex >= $scope.page) {//判断最大页码
                    $scope.requestArr.pageIndex = $scope.page;
                    return false;
                }
                $scope.requestArr.pageIndex++
                requestProductList();
            }
        }
        //end
        $scope.end = function () {
            if ($scope.page) {
                $scope.requestArr.pageIndex = $scope.page;
                requestProductList();
            }
        }
        //上一页
        $scope.prev = function () {
            if ($scope.requestArr.pageIndex <= 1) {//判断最小页码
                $scope.requestArr.pageIndex = 1;
                return false;
            }
            $scope.requestArr.pageIndex--;
            requestProductList()
        }
        $scope.requestArr = {//查询条件
            ProductName: '',
            ProductCode: '',
            ProductGroup: '0',
            UserId: '',
            ProductStatus: '0',
            IsSq: '',
            FullstartTime: '',
            FullendTime: '',
            pageIndex: 1,
            pageSize: 10
        }
        //导出
        $scope.export = function () {
            if ($scope.requestArr.FullendTime == undefined || $scope.requestArr.FullendTime == '') {
                $scope.requestArr.FullendTime = ''
            } else {
                $scope.requestArr.FullendTime = commonFactory.DateFormat($scope.requestArr.FullendTime, 1);
            }
            if ($scope.requestArr.FullstartTime == undefined || $scope.requestArr.FullstartTime == "") {
                $scope.requestArr.FullstartTime = ''
            } else {
                $scope.requestArr.FullstartTime = commonFactory.DateFormat($scope.requestArr.FullstartTime, 1)
            }
            if ($scope.requestArr.ProductName == undefined || $scope.requestArr.ProductName == '') {
                $scope.requestArr.ProductName = ''
            }
            if ($scope.requestArr.ProductCode == undefined || $scope.requestArr.ProductCode == '') {
                $scope.requestArr.ProductCode = ''
            }
            if ($scope.requestArr.UserId == undefined || $scope.requestArr.UserId == '') {
                $scope.requestArr.UserId = ''
            }
            if ($scope.requestArr.IsSq == undefined || $scope.requestArr.IsSq == '') {
                $scope.requestArr.IsSq = ''
            }
            if ($scope.requestArr.ProductGroup == undefined || $scope.requestArr.ProductGroup == '') {
                $scope.requestArr.ProductGroup = ''
            }
            $scope.gourl = commonFactory.baseurl + '/api/Product/Expoit?FullendTime=' + $scope.requestArr.FullendTime + '&FullstartTime=' + $scope.requestArr.FullstartTime + '&IsSq=' + $scope.requestArr.IsSq + '&ProductCode=' + $scope.requestArr.ProductCode + '&ProductGroup=' + $scope.requestArr.ProductGroup + '&ProductName=' + $scope.requestArr.ProductName + '&ProductStatus=' + $scope.requestArr.ProductStatus + '&UserId=' + $scope.requestArr.UserId + '&pageIndex=' + $scope.requestArr.pageIndex + '&pageSize=' + $scope.requestArr.pageSize
        }

        //查询
        $scope.search = function () {
            $scope.graybtn = true;
            $scope.requestArr.pageIndex = 1 //查询后在第一页显示
            if (!$scope.requestArr.FullstartTime) {//处理时间NaN
                $scope.requestArr.FullstartTime = ''
            } else {
                $scope.requestArr.FullstartTime = commonFactory.DateFormat($scope.requestArr.FullstartTime, 1)
            }
            if (!$scope.requestArr.FullendTime) {
                $scope.requestArr.FullendTime = ''
            } else {
                $scope.requestArr.FullendTime = commonFactory.DateFormat($scope.requestArr.FullendTime, 1)
            }

            requestProductList();
        }
        // 删除缓存
        $scope.delCache = function () {
            var uId = localStorage.getItem('u_id');
            $.get(commonFactory.baseurl + "/api/common/delkey?key=217774AD-C4ED-4748-AD51-568CCAD1665D", { u_id: uId }, function (result) {
            })
            $.get(commonFactory.baseurl + "/api/common/delkey?key=BD08CE9B-6F15-4A75-B1F0-76401D1E7C3F|*", { u_id: uId }, function (result) {
            })
            $.get(commonFactory.baseurl + "/api/common/delkey?key=A5FD8A29-8207-4910-897A-AB21379DABD5", { u_id: uId }, function (result) {
            })
            // $.get(commonFactory.baseurl + "/api/common/delkey?key=89BA5DA9-264F-4629-98E4-53547481A3A9|*", { u_id: uId }, function (result) {
            // })
        }
        var requestProductList = function () {
            commonFactory.loading(true)
            commonFactory.ajax("/api/product/QueryLoanProductList", $scope.requestArr, function (result) {
                $scope.graybtn = false;
                commonFactory.loading(false)
                if (result.Code == 1) {
                    $scope.QueryLoanProductList = result.RealyList;
                    $scope.QueryLoanProductListTotal = result.Total;
                    $scope.TotalFullPrice = result.TotalFullPrice;
                    $scope.TotalLoanPrice = result.TotalLoanPrice;
                    $scope.page = Math.ceil(result.Total / $scope.requestArr.pageSize);
                    $scope.status = function (sta) {
                        switch (sta) {
                            case 1: return '未发布';
                            case 2: return '已发布';
                            case 3: return '募资中';
                            case 4: return '已满标';
                            case 5: return '还款中';
                            case 6: return '提前还清';
                            case 7: return '已还清';
                            case 8: return '等待推送至银行';
                            case 13: return '流标';
                            case 15: return '草稿';
                            case 16: return '已删除';
                        }
                    }
                    $scope.productgroup = function (sta) {
                        switch (sta) {
                            case 1: return '新手标';
                            case 18: return '一月';
                            case 19: return '六月';
                            case 20: return '三月';
                        }
                    }
                    $scope.productRepaymentTypeId = function (id) {
                        switch (id) {
                            case 1: return '等额本息';
                            case 3: return '先息后本';
                            case 4: return '到期还本付息';
                        }
                    }

                } else {
                    toaster.pop('error', '失败', result.ResposeData);
                }
            }, function () {
                $scope.graybtn = false;
                toaster.pop('error', '失败', '后台未返回报错');
            }, "post");
        }
        requestProductList();
        //募资中
        $scope.Raise = function (pId) {
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                controller: ['$scope', '$modalInstance', 'raise', function ($scope, $modalInstance, raise) {
                    $scope.raise = raise;
                    $scope.ok = function () {
                        commonFactory.loading(true)
                        commonFactory.ajax("/api/product/ErpSetMuZi", { '': $scope.raise }, function (result) {
                            commonFactory.loading(false)
                            if (result.Code == 1) {
                                toaster.pop('success', '成功', '募资成功');
                                $modalInstance.close();
                            } else {
                                toaster.pop('error', '失败', decodeURIComponent(result.ResposeData));
                                $modalInstance.close();
                            }
                        }, function () {
                            toaster.pop('error', '失败', '后台返回报错');
                        }, "post");
                    }
                    $scope.cancel = function () {
                        $modalInstance.close();
                    };
                }
                ],
                size: 'sm',
                backdrop: false,
                resolve: {
                    raise: function () {
                        return pId;
                    }
                }
            });
            modalInstance.result.then(function () {
                requestProductList();
            }, function () {
            });
        }
        //发布
        $scope.Release = function (pid) {
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                controller: ['$scope', '$modalInstance', 'Release', function ($scope, $modalInstance, Release) {
                    $scope.Release = Release;
                    $scope.ok = function () {
                        commonFactory.loading(true)
                        commonFactory.ajax("/api/Product/ReleaseProduct", { '': $scope.Release }, function (result) {
                            commonFactory.loading(false)
                            if (result.Code == 1) {
                                toaster.pop('success', '成功', '发布成功');
                                $modalInstance.close();
                            } else {
                                toaster.pop('error', '失败', decodeURIComponent(result.ResposeData));
                                $modalInstance.close();
                            }
                        }, function () {
                            toaster.pop('error', '失败', '后台未返回报错');
                        }, "post");
                    }
                    $scope.cancel = function () {
                        $modalInstance.close();
                    };
                }
                ],
                size: 'sm',
                backdrop: false,
                resolve: {
                    Release: function () {
                        return pid;
                    }
                }
            });
            modalInstance.result.then(function () {
                requestProductList();
            }, function () {
            });

        }
        //撤标
        $scope.Retract = function (pid) {
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                controller: ['$scope', '$modalInstance', 'Retract', function ($scope, $modalInstance, Retract) {
                    $scope.Retract = Retract;
                    $scope.ok = function () {
                        commonFactory.loading(true)
                        commonFactory.ajax("/api/Product/RetractProduct", { '': $scope.Retract }, function (result) {
                            commonFactory.loading(false)
                            if (result.Code == 1) {
                                toaster.pop('success', '成功', '撤标成功');
                                $modalInstance.close();
                            } else {
                                toaster.pop('error', '失败', decodeURIComponent(result.ResposeData));
                                $modalInstance.close();
                            }
                        }, function () {
                            toaster.pop('error', '失败', '后台未返回报错');
                        }, "post");
                    }
                    $scope.cancel = function () {
                        $modalInstance.close();
                    };
                }
                ],
                size: 'sm',
                backdrop: false,
                resolve: {
                    Retract: function () {
                        return pid;
                    }
                }
            });
            modalInstance.result.then(function () {
                requestProductList();
            }, function () {
            });

        }
        //编辑
        $scope.edit = function (pid) {
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                controller: 'editproduct',
                size: 'lg',
                backdrop: true,
                resolve: {
                    edit: function () {
                        return pid;
                    }
                }
            });
            modalInstance.result.then(function () {
                requestProductList();
            }, function () {
            });
        }
        // 添加
        $scope.addproductd = function () {
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                controller: 'addproduct',
                size: 'lg',
                backdrop: true
            });
            modalInstance.result.then(function () {
                requestProductList();
            }, function () {
            });
        }
        //合同
        $scope.contract = function (pid) {
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                controller: 'contract',
                size: 'lg',
                backdrop: true,
                resolve: {
                    contract: function () {
                        return pid;
                    }
                }
            });
            modalInstance.result.then(function () {
                requestProductList();
            }, function () {
            });
        }
        // 删除
        $scope.delete = function (obj) {
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                controller: ['$scope', '$modalInstance', 'items', 'toaster', 'commonFactory', function ($scope, $modalInstance, items, toaster, commonFactory) {
                    $scope.items = items;
                    $scope.ok = function () {
                        //删除
                        commonFactory.loading(true)
                        commonFactory.ajax("/api/Product/DeleteProduct", { '': $scope.items.Id }, function (result) {
                            commonFactory.loading(false)
                            if (result.Code == 1) {
                                toaster.pop('success', '成功', '删除成功');
                                $modalInstance.close();
                            } else {
                                toaster.pop('error', '失败', decodeURIComponent(result.ResposeData));
                                $modalInstance.close();
                            }
                        }, function () {
                            toaster.pop('error', '失败', '后台未返回报错');
                        }, "post");
                    }

                    $scope.cancel = function () {
                        $modalInstance.close('cancel');
                    };
                }],
                size: 'sm',
                backdrop: false,
                resolve: {
                    items: function () {
                        return obj;
                    }
                }
            });
            modalInstance.result.then(function () {
                requestProductList();
            }, function () {

            });
        };
        // 满标
        $scope.manb = function (obj) {
            $scope.manbitem = {
                canbuyPrice: '',
                id: obj.Id
            }
            commonFactory.ajax("/api/Product/GetSurplusCanBuyPrice", { '': obj.Id }, function (result) {
                $scope.manbitem.canbuyPrice = result;
                var modalInstance = $modal.open({
                    templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                    controller: ['$scope', '$modalInstance', 'manbitem', function ($scope, $modalInstance, manbitem) {
                        $scope.manbShow = true
                        $scope.manbitem = manbitem
                        $scope.manbData = {
                            ProductId: manbitem.id,
                            Price: ""
                        }
                        var checkprice = function () {
                            if ($scope.manbData.Price == "") {
                                toaster.pop('error', '提示', '请输入满标金额');
                                return false
                            }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
                            if (obj.ProductGroupId == 1 && $scope.manbData.Price > 50000){
                                toaster.pop('error', '提示', '输入的金额超限，请重新输入');
                                return false  
                            }
                            return true;
                        }
                        $scope.ok = function () {
                            //满标
                            if (checkprice()) {
                                $('.diabledBtn').attr("disabled", "disabled");
                                commonFactory.loading(true)
                                commonFactory.ajax("/api/Product/ErpCrateOrderItem", $scope.manbData, function (result) {
                                    commonFactory.loading(false)
                                    $('.diabledBtn').removeAttr("disabled");
                                    if (result.Code == 1) {
                                        toaster.pop('success', '成功', '满标成功');
                                        $modalInstance.close();
                                    } else {
                                        toaster.pop('error', '失败', decodeURIComponent(result.ResposeData));
                                        $modalInstance.close();
                                    }
                                }, function () {
                                    commonFactory.loading(false)
                                    $('.diabledBtn').removeAttr("disabled");
                                    toaster.pop('error', '失败', '后台未返回报错');
                                }, "post");
                            }

                        }
                        $scope.cancel = function () {
                            $modalInstance.close();
                        };
                    }
                    ],
                    size: 'sm',
                    backdrop: false,
                    resolve: {
                        manbitem: function () {
                            return $scope.manbitem;
                        }
                    }
                });
                modalInstance.result.then(function () {
                    requestProductList();
                }, function () {
                });
            }, function () {
                toaster.pop('error', '失败', '后台未返回报错');
            }, "post");
        };
        //借款人
        $scope.goAdd = function (id) {
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                controller: 'borrow',
                size: 'lg',
                backdrop: true,
                resolve: {
                    goAdd: function () {
                        return id;
                    }
                }
            });
            modalInstance.result.then(function () {
                requestProductList();
            }, function () {
            });

        }
        //一键还款
        $scope.hcash = function (pid) {
            commonFactory.ajax("/api/Product/GetLoanContractsByProductId", { '': pid }, function (result) {
                $scope.formdata = result.Result;
                $timeout(function () {
                    var formObject = document.querySelector('#idmyForm')
                    formObject.action = result.Url
                    formObject.submit();
                }, 50)
            }, function () { }, 'post')
        }
        // 是否授权
        $scope.editstatus = function (id, status) {
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                controller: ['$scope', '$modalInstance', 'editstatus','$timeout', function ($scope, $modalInstance, editstatus,$timeout) {
                    $scope.editstatusid = id;//弹出框判断
                    $scope.editstatus = editstatus;//绑定状态
                    var off = true;
                    $timeout(function(){
                        $('#hideInput').focus()
                    },100)
                    
                    $scope.ok = function () {
                        if (!off) { return false }
                        off = false;
                        commonFactory.loading(true)
                        commonFactory.ajax("/api/product/EditProductAuthStatus", { '': id }, function (result) {
                            commonFactory.loading(false)
                            toaster.pop('success', '成功', '修改成功');
                            $modalInstance.close();
                            // if (result.code == 1) {

                            // } else {
                            //     toaster.pop('error', '', '失败');
                            //     $modalInstance.close();
                            // }
                        }, function () {
                            commonFactory.loading(false)
                            toaster.pop('error', '失败', '后台未返回报错');
                        }, "post");
                    }
                    $scope.cancel = function () {
                        $modalInstance.close();
                    };
                    $scope.loginevent = function (e) {
                        if (e.keyCode == 13) {
                            $scope.ok();
                        }
                    }
                }
                ],
                size: 'sm',
                backdrop: false,
                resolve: {
                    editstatus: function () {
                        return status;
                    }
                }
            });
            modalInstance.result.then(function () {
                requestProductList();
            }, function () {
            });
            
        }
        // 电子合同下载
        $scope.downContract = function(pid){
            var uId = localStorage.getItem('u_id');
            $.ajax({
                type:"get",
                url:commonFactory.baseurl + "/api/Contracts/GetContractPage", 
                data:{ u_id: uId, productId: pid }, 
                async:false,
                success:function (result) {
                    if(result.Code == 1 && result.ResposeData > 0){
                        for (var i = 0; i < result.ResposeData; i++) {
                            window.open(commonFactory.baseurl + "/api/Contracts/DownContract?productId="+pid+"&u_id="+uId+"&pageIndex="+(i+1))

                        }
                    }else{
                        $timeout(function() {
                            toaster.pop('warning', '警告', '可下载次数为0');
                        }, 500);
                    }
                }
            })
        }

        // 手动满标
        $scope.fullScale = function(pid){
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                controller: ['$scope', '$modalInstance', 'pid', function ($scope, $modalInstance, pid) {
                    $scope.fullscaleShow = pid;
                    $scope.ok = function () {
                        commonFactory.loading(true)
                        commonFactory.ajax("/api/product/ErpSetFull", { "" : pid }, function (result) {
                            commonFactory.loading(false)
                            if (result.Code == 1) {
                                toaster.pop('success', '成功', '满标成功');
                                $modalInstance.close();
                            } else {
                                toaster.pop('error', '失败', decodeURIComponent(result.ResposeData));
                                $modalInstance.close();
                            }
                        }, function () {
                            toaster.pop('error', '失败', '后台未返回报错');
                        }, "post");
                    }
                    $scope.cancel = function () {
                        $modalInstance.close();
                    };
                }
                ],
                size: 'sm',
                backdrop: false,
                resolve: {
                    pid: function () {
                        return pid;
                    }
                }
            });
            modalInstance.result.then(function () {
                requestProductList();
            }, function () {
            });
            // commonFactory.ajax("/api/Flexible/FullProductBySystem", { '': pid }, function (result) {
            //     $scope.formdata = result.Result;
            //     $timeout(function () {
            //         var formObject = document.querySelector('#idmyForm')
            //         formObject.action = result.Url
            //         formObject.submit();
            //     }, 50)
            // }, function () { }, 'post')
        }
        
    }

    initdata();
}]);
// 添加产品
app.controller('addproduct', ['$scope', '$http', 'commonFactory', 'toaster', '$timeout', '$modalInstance', function ($scope, $http, commonFactory, toaster, $timeout, $modalInstance) {
    $scope.addproduct = true
    $scope.allData = {
        ProductType:'02',
        ProductCode: "",
        ProductName: "",
        ProductGroupId: "18",
        UserId: "",
        LoanPrice: "",
        ContractNo: "",
        AnnualRateOfYear: "",
        LoanPeriod: "",
        LoanPeriodUnitId: "1",
        RepaymentTypeId: "1",
        ProductStatus: "",
        // BusinessTypeId: "1",
        BidType:"03",
        PublishTime: "",
        ExpireTime: '2020-01-01',
        RepayDate: '2020-01-02',
        FirstOnlineTime: "",
        FullTime: "",
        ProductSortId: "",
        // IsDeleted
        PropertyList: [],
        ShowRate: "",
        Subscript: "0",
        MinInvestPrice: "",
        MaxInvestPrice: "",
        CountDown: 0,
        Description: "",
        BorrPurpose:"",
        fileId: "",
        FileUrl:"",
        LoanBrower:''
    }
    $scope.checkRate=function(){
        if($scope.allData.AnnualRateOfYear<=7||$scope.allData.AnnualRateOfYear>=20){
            $scope.text='输入范围有误，请重新输入'
        }else{
            $scope.text=''
        }
    }
    $scope.nocilck = true
    $scope.addProduct = function () {
        var all = $(".forminfo")
        var number = $(".forminfo1[type='number']")
        for (var i = all.length - 1; i >= 0; i--) {
            if ($(all[i]).val() == "? string: ?" || $(all[i]).val() == "") {
                toaster.pop('warning', '', '数据不能为空');
                return;
            }
        }
        for (var i = number.length - 1; i >= 0; i--) {
            if ($(number[i]).val() % 1 != 0) {
                toaster.pop('warning', '', '数值必须为整数');
                return;
            }
        }
        if (!$scope.allData.PhotoUrl) {
            toaster.pop('warning', '', '请上传图片');
            return;
        }
        if($scope.allData.AnnualRateOfYear<=7||$scope.allData.AnnualRateOfYear>=20){
            toaster.pop('warning', '', '利率输入有误');
            return;
        }
        $scope.allData.BorrPurpose = $scope.allData.BorrPurpose.trim()
        if($scope.allData.BorrPurpose.length>150){
            toaster.pop('warning', '', '借款用途字数不得超过150个，当前字数为'+$scope.allData.BorrPurpose.trim().length);
            return;
        }
        if ($scope.allData.PropertyList) {
            for (var i = $scope.allData.PropertyList.length - 1; i >= 0; i--) {
                if ($scope.allData.PropertyList[i].indexOf(".jpg") == -1 && $scope.allData.PropertyList[i].indexOf(".png") == -1 && $scope.allData.PropertyList[i].indexOf(".JPG") == -1 && $scope.allData.PropertyList[i].indexOf(".PNG") == -1) {
                    toaster.pop('warning', '', '上传图片中包含非jpg、png格式，请重新选择上传');
                    return;
                }
            }
        }
        if ($scope.allData.FileUrl && $scope.allData.FileUrl[0].indexOf(".zip") == -1) {
            toaster.pop('warning', '', '文件格式不符，只能上传.ZIP文件');
            return;
        }
        $scope.allData.ExpireTime = commonFactory.DateFormat($scope.allData.ExpireTime)
        $scope.allData.RepayDate = commonFactory.DateFormat($scope.allData.RepayDate)
        $scope.allData.Description = encodeURIComponent($scope.allData.Description)
        $scope.allData.LoanBrower = encodeURIComponent($scope.allData.LoanBrower)
        $scope.nocilck = false
        commonFactory.loading(true)
        commonFactory.ajax("/api/product/AddLoanProduct", $scope.allData, function (result) {
            $scope.nocilck = true;
            commonFactory.loading(false)
            if (result.Code == 1) {
                toaster.pop('success', '', '添加成功');
                $modalInstance.close()
            } else {
                toaster.pop('error', '', '添加失败');
            }
        }, function () {
            commonFactory.loading(false)
            toaster.pop('error', '', '后台报错');
        }, "post");
    }
    $scope.mobile = { mobile: '' };
    $scope.getUserId = function () {
        if ($scope.mobile.mobile == "") {
            toaster.pop('warning', '', '查询内容不能为空');
        } else {
            commonFactory.loading(true)
            commonFactory.ajax("/api/product/ErpGetLoanPersonByMobile", { '': $scope.mobile.mobile }, function (result) {
                commonFactory.loading(false)
                if (result == null) {
                    toaster.pop('warning', '', '查无此人');
                } else {
                    $scope.allData.UserId = result.Id;
                }
            }, function () {
                commonFactory.loading(false)
                toaster.pop('warning', '', '后台报错');
            }, "post");
        }
    }

    $scope.loginevent = function (e) {
        if (e.keyCode == 13) {
            $scope.getUserId();
        }
    }
}]);
// 编辑产品editproduct
app.controller('editproduct', ['$rootScope','$scope', '$http', 'commonFactory', 'toaster', '$timeout', 'edit', '$modalInstance', '$interval', function ($rootScope, $scope, $http, commonFactory, toaster, $timeout, edit, $modalInstance, $interval) {
    $scope.productId = edit;
    $scope.allData = {
        UserId: ''
    }
    // 通过id 获取 要编辑产品数据
    commonFactory.ajax("/api/Product/GetProductModelByID", { '': $scope.productId }, function (result) {
        $scope.allData = result;
        $scope.allDatalist = result.PropertyList;
        if (result == null) {
            toaster.pop('warning', '', '未查询到此Id');
        }
    }, function () {
        toaster.pop('error', '', '后台报错');
    }, "post");

    var interval = $interval(function () {
        if ($scope.allData.PhotoUrl) {
            $interval.cancel(interval);
            $scope.imgurlad = $scope.allData.PhotoUrl
        }
    }, 100)
    var zipInterval = $interval(function () {
        if ($scope.allData.FileUrl) {
            $interval.cancel(zipInterval);
            $scope.FileUrl = $scope.allData.FileUrl
        }
    }, 100)
    $scope.rightClick = function(e,fileUrl){
        if(e.which==3){
            window.oncontextmenu = function (){
                return false;     // 取消默认菜单
            }
            window.open($rootScope.imgurl+fileUrl)
        }
    }
    $scope.editProduct = function () {
        var all = $(".forminfo")
        var number = $(".forminfo1[type='number']")
        for (var i = all.length - 1; i >= 0; i--) {
            if ($(all[i]).val() == "? string: ?" || $(all[i]).val() == "? object:null ?" || $(all[i]).val() == "") {
                toaster.pop('warning', '', '数据不能为空');
                return;
            }
        }
        for (var i = number.length - 1; i >= 0; i--) {
            if ($(number[i]).val() % 1 != 0) {
                toaster.pop('warning', '', '数值必须为整数');
                return;
            }
        }
        if($scope.allData.AnnualRateOfYear<=7||$scope.allData.AnnualRateOfYear>=20){
            toaster.pop('warning', '', '利率输入有误');
            return;
        }
        $scope.allData.BorrPurpose = $scope.allData.BorrPurpose.trim()
        if($scope.allData.BorrPurpose.length>150){
            toaster.pop('warning', '', '借款用途字数不得超过150个，当前字数为'+$scope.allData.BorrPurpose.trim().length);
            return;
        }
        if ($scope.allData.PhotoUrl) {
            for (var i = $scope.allData.PhotoUrl.length - 1; i >= 0; i--) {
                if ($scope.allData.PhotoUrl[i].indexOf(".jpg") == -1 && $scope.allData.PhotoUrl[i].indexOf(".png") == -1 && $scope.allData.PhotoUrl[i].indexOf(".JPG") == -1 && $scope.allData.PhotoUrl[i].indexOf(".PNG") == -1) {
                    toaster.pop('warning', '', '上传图片中包含非jpg、png格式，请重新选择上传');
                    return;
                }
            }
        }
        if ($scope.allData.FileUrl && $scope.allData.FileUrl[0].indexOf(".zip") == -1) {
            toaster.pop('warning', '', '文件格式不符，只能上传.ZIP文件');
            return;
        }


        $scope.allData.ExpireTime = commonFactory.DateFormat($scope.allData.ExpireTime)
        $scope.allData.RepayDate = commonFactory.DateFormat($scope.allData.RepayDate);
        $scope.allData.Description = encodeURIComponent($scope.allData.Description)
        $scope.allData.LoanBrower = encodeURIComponent($scope.allData.LoanBrower)
        if ($scope.allData.PhotoUrl) {
            $scope.allData.PhotoUrl = $scope.allData.PhotoUrl.toString();
        } else {
            var arr = [];
            $scope.allDatalist.forEach(function (ele) {
                if(ele.Type == 1&&ele.Url&&ele.Url!="null"){
                    arr.push(ele.Url)
                }
            });
            $scope.allData.PhotoUrl = arr
        }
        if ($scope.allData.FileUrl) {
            $scope.allData.FileUrl = $scope.allData.FileUrl.toString();
        } else {
            var ziparr = [];
            $scope.allDatalist.forEach(function (ele) {
                if(ele.Type == 5&&ele.Url&&ele.Url!="null"){
                    ziparr.push(ele.Url)
                }
            });
            $scope.allData.FileUrl = ziparr
        }
        if ($scope.borrow) {
            $scope.allData.UserId = $scope.borrow;
        }
        commonFactory.loading(true)
        commonFactory.ajax("/api/Product/EditLoanProduct", $scope.allData, function (result) {
            commonFactory.loading(false)
            if (result.Code == 1) {
                localStorage.getItem("editProductId", "");
                toaster.pop('success', '', '编辑成功');
                $modalInstance.close();
            } else {
                toaster.pop('error', '失败', decodeURIComponent(result.ResposeData));
                $modalInstance.close();
            }
        }, function () {
            commonFactory.loading(false)
            toaster.pop('error', '', '后台报错');
        }, "post");
    }
    $scope.checkRate=function(){//限制输入利率范围
        if($scope.allData.AnnualRateOfYear<=7||$scope.allData.AnnualRateOfYear>=20){
            $scope.text='输入范围有误，请重新输入'
        }else{
            $scope.text=''
        }
    }

    $scope.mobile = { mobile: '' };
    $scope.getUserId = function () {
        if ($scope.mobile.mobile == "") {
            toaster.pop('warning', '', '查询内容不能为空');
        } else {
            commonFactory.loading(true)
            commonFactory.ajax("/api/product/ErpGetLoanPersonByMobile", { '': $scope.mobile.mobile }, function (result) {
                commonFactory.loading(false)
                if (result == null) {
                    toaster.pop('warning', '', '查无此人');
                } else {
                    if (!$scope.allData) {
                        $scope.borrow = result.Id;
                    } else {
                        $scope.allData.UserId = result.Id;
                    }
                }
            }, function () {
                commonFactory.loading(false)
            }, "post");

        }
    }
    $scope.loginevent = function (e) {
        if (e.keyCode == 13) {
            $scope.getUserId();
        }
    }
}]);
//借款人信息
app.controller('borrow', ['$scope', '$http', 'commonFactory', 'toaster', '$timeout', '$modalInstance', 'goAdd', '$interval', function ($scope, $http, commonFactory, toaster, $timeout, $modalInstance, goAdd, $interval) {
    $scope.goAdd = goAdd;
    $scope.allinp = {
        UserName: '',
        Mobile: '',
        IdCard: '',
        Sex: '',
        Age: '',
        CardUrl: '',
        productId: ''
    }
    var interval = $interval(function () {
        if ($scope.allinp.CardUrl) {
            $interval.cancel(interval);
            if (typeof ($scope.allinp.CardUrl) == 'string') {
                $scope.imgurlad = $scope.allinp.CardUrl
            } else {
                $scope.imgurlad = $scope.allinp.CardUrl[0]
            }
        }
    }, 500)
    var checkarr = function () {
        if (!$scope.allinp.Mobile) {
            toaster.pop('warning', '', '请填写手机号');
            return;
        }
        if (!$scope.allinp.IdCard) {
            toaster.pop('warning', '', '请填写身份证号');
            return;
        }
        if (typeof ($scope.allinp.CardUrl) == 'string') {
            if ($scope.allinp.CardUrl && $scope.allinp.CardUrl.indexOf(".jpg") == -1 && $scope.allinp.CardUrl.indexOf(".png") == -1 && $scope.allinp.CardUrl.indexOf(".JPG") == -1 && $scope.allinp.CardUrl.indexOf(".PNG") == -1) {
                toaster.pop('warning', '', '上传图片中包含非jpg、png格式，请重新选择上传');
                return;
            }
        }
        if ($scope.allinp.CardUrl && $scope.allinp.CardUrl[0].indexOf(".jpg") == -1 && $scope.allinp.CardUrl[0].indexOf(".png") == -1 && $scope.allinp.CardUrl[0].indexOf(".JPG") == -1 && $scope.allinp.CardUrl[0].indexOf(".PNG") == -1) {
            toaster.pop('warning', '', '上传图片中包含非jpg、png格式，请重新选择上传');
            return;
        }
        if (!$scope.allinp.Age) {
            toaster.pop('warning', '', '请填写年龄');
            return;
        }
        if (!$scope.allinp.UserName) {
            toaster.pop('warning', '', '请填写姓名');
            return;
        }
        return true;
    }

    var proId = goAdd;
    $scope.allinp.productId = goAdd
    $scope.addborrow = function () {//添加借款人信息
        if (checkarr()) {
            if(!$scope.allinp.CardUrl){
                toaster.pop('warning', '', '请添加图片');
                return false
            }
            commonFactory.loading(true)
            commonFactory.ajax("/api/Product/AddLoanInfoShow", $scope.allinp, function (result) {
                commonFactory.loading(false)
                if (result == null) {
                    toaster.pop('error', '', '添加失败');
                } else {
                    toaster.pop('success', '', '添加成功');
                    $modalInstance.close();
                }
            }, function () {
                commonFactory.loading(false)
                toaster.pop('error', '', '后台错误');
            }, "post");
        };
    }
    $scope.addga = function () {//修改借款人
        if (checkarr()) {
            if ($scope.allinp.CardUrl) {
                $scope.allinp.CardUrl = $scope.allinp.CardUrl.toString();
            } else if (!$scope.allinp.CardUrl&&$scope.GetLoanInfoByProductID.CardUrl){
                $scope.allinp.CardUrl = $scope.GetLoanInfoByProductID.CardUrl.toString()
            }else{
                toaster.pop('warning', '', '请添加图片');
                return false
            }
            if ($scope.allinp.Sex == 'true') {
                $scope.allinp.Sex = true
                $scope.man = true;
            } else {
                $scope.allinp.Sex = false
                $scope.wmen = true
            }
            $scope.allinp.productId = goAdd;
            commonFactory.loading(true)
            commonFactory.ajax("/api/Product/EditLoanInfoShow", $scope.allinp, function (result) {
                commonFactory.loading(false)
                if (result == null) {
                    toaster.pop('error', '', '修改失败');
                } else {
                    if (result.Code == 1) {
                        toaster.pop('success', '', '修改成功');
                        $modalInstance.close();
                    } else {
                        toaster.pop('error', '', decodeURIComponent(result.ResposeData));
                    }
                }
            }, function () {
                commonFactory.loading(false)
                toaster.pop('error', '', '后台错误');
            }, "post");
        };
    }
    commonFactory.ajax("/api/Product/GetLoanInfoByProductID", { '': proId }, function (result) {//在页面绑定数据
        if (result.Data == null) {
            // toaster.pop('error', '', '返回为null');
        } else {
            $scope.GetLoanInfoByProductID = result.Data;
            $scope.allinp = {
                UserName: $scope.GetLoanInfoByProductID.UserName,
                Mobile: $scope.GetLoanInfoByProductID.Mobile,
                IdCard: $scope.GetLoanInfoByProductID.IdCard,
                Age: $scope.GetLoanInfoByProductID.Age
            }
            if ($scope.GetLoanInfoByProductID.Sex) {
                $scope.man = true;
            } else {
                $scope.wmen = true
            }
        }

    }, function () {
        toaster.pop('error', '', '后台错误');
    }, "post");
}]);
//合同
app.controller('contract', ['$scope', '$http', 'commonFactory', 'toaster', '$timeout', 'contract', '$modalInstance', function ($scope, $http, commonFactory, toaster, $timeout, contract, $modalInstance) {
    $scope.contract = contract;
    $scope.allinp = {
        ProductCode: "",
        ContractCode: "",
        Purpose: "资金周转",
        PartyAName: "",
        PartyAIdCard: "",
        PartyCName: "",
        PartyDName: "北京有利金服技术服务有限公司",
        Id: ""
    }
    var checkarr = function () {
        if (!$scope.allinp.ProductCode) {
            toaster.pop('error', '', '请填写产品编号');
            return;
        }
        if (!$scope.allinp.ContractCode) {
            toaster.pop('error', '', '请填写合同编号');
            return;
        }
        if (!$scope.allinp.Purpose) {
            toaster.pop('error', '', '请填写资金用途');
            return;
        }
        if (!$scope.allinp.PartyAIdCard) {
            toaster.pop('error', '', '请填写甲方身份证号');
            return;
        }
        if (!$scope.allinp.PartyAName) {
            toaster.pop('error', '', '请填写甲方姓名');
            return;
        }
        if (!$scope.allinp.PartyDName) {
            toaster.pop('error', '', '请填写丁方名称');
            return;
        }
        if (!$scope.allinp.PartyCName) {
            toaster.pop('error', '', '请填写丙方姓名');
            return;
        }
        return true;
    }
    var proId = contract;
    $scope.allinp.ProductCode = proId;
    $scope.addborrow = function () {//添加合同
        if (checkarr()) {
            commonFactory.loading(true)
            commonFactory.ajax("/api/Product/AddProductContract", $scope.allinp, function (result) {
                commonFactory.loading(false)
                if (result == null) {
                    toaster.pop('error', '', '添加失败');
                } else {
                    toaster.pop('success', '', '添加成功');
                    $modalInstance.close();
                }
            }, function () {
                commonFactory.loading(false)
                toaster.pop('error', '', '后台错误');
            }, "post");
        };
    }
    $scope.addga = function () {//修改合同
        if (checkarr()) {
            commonFactory.loading(true)
            commonFactory.ajax("/api/Product/EditProductContract", $scope.allinp, function (result) {
                commonFactory.loading(false)
                if (result == null) {
                    toaster.pop('error', '', '修改失败');
                } else {
                    if (result.Code == 1) {
                        toaster.pop('success', '', '修改成功');
                        $modalInstance.close();
                    } else {
                        toaster.pop('error', '', result.ResposeData);
                        $modalInstance.close();
                    }
                }
            }, function () {
                commonFactory.loading(false)
                toaster.pop('error', '', '后台错误');
            }, "post");
        };
    }

    commonFactory.ajax("/api/Product/GetProductContractByCode", { '': proId }, function (result) {//在页面绑定数据
        if (result == null) {
            // toaster.pop('error', '', '返回为null');
        } else {
            $scope.GetProductContractByCode = result;
            $scope.allinp = {
                ProductCode: proId,
                ContractCode: $scope.GetProductContractByCode.ContractCode,
                Purpose: '资金周转',
                PartyAName: $scope.GetProductContractByCode.PartyAName,
                PartyAIdCard: $scope.GetProductContractByCode.PartyAIdCard,
                PartyCName: $scope.GetProductContractByCode.PartyCName,
                PartyDName: "北京有利金服技术服务有限公司"
            }
            if (!$scope.allinp.Sex) {
                $scope.man = true;
            } else {
                $scope.wmen = false
            }
        }
    }, function () {
        toaster.pop('error', '', '后台错误');
    }, "post");
    $scope.mobile = { mobile: '' };
    $scope.getUserId = function () {
        if ($scope.mobile.mobile == "") {
            toaster.pop('warning', '', '查询内容不能为空');
        } else {
            commonFactory.loading(true)
            commonFactory.ajax("/api/product/ErpGetLoanPersonByMobile", { '': $scope.mobile.mobile }, function (result) {
                commonFactory.loading(false)
                if (result == null) {
                    toaster.pop('warning', '', '查无此人');
                } else {
                    if (!$scope.allData) {
                        $scope.searchName = result;
                        $scope.borrow = result.Id;
                        $scope.allinp.PartyAName = $scope.searchName.FullName;
                        $scope.allinp.PartyAIdCard = $scope.searchName.IdCard;
                    } else {
                        $scope.allData.UserId = result.Id;
                    }
                }
            }, function () {
                commonFactory.loading(false)
            }, "post");

        }
    }
    $scope.loginevent = function (e) {
        if (e.keyCode == 13) {
            $scope.getUserId();
        }
    }
}]);
//活期产品列表
app.controller('producthuo', ['$scope', '$http', 'commonFactory', 'toaster', '$timeout', '$interval', '$modal', function ($scope, $http, commonFactory, toaster, $timeout, $interval, $modal) {
    var initdata = function () {
        $scope.godata = function () {//页面size
            requestProductList()
        }
        //下一页
        $scope.next = function () {
            if ($scope.page) {
                if ($scope.requestArr.pageIndex >= $scope.page) {//判断最大页码
                    $scope.requestArr.pageIndex = $scope.page;
                    return false;
                }
                $scope.requestArr.pageIndex++
                requestProductList();
            }
        }
        //end
        $scope.end = function () {
            if ($scope.page) {
                $scope.requestArr.pageIndex = $scope.page;
                requestProductList();
            }
        }
        //上一页
        $scope.prev = function () {
            if ($scope.requestArr.pageIndex <= 1) {//判断最小页码
                $scope.requestArr.pageIndex = 1;
                return false;
            }
            $scope.requestArr.pageIndex--;
            requestProductList()
        }
        $scope.requestArr = {//查询条件
            ProductName: '',
            ProductCode: '',
            ProductGroup: '',
            UserId: '',
            ProductStatus: '',
            FullstartTime: '',
            FullendTime: '',
            IsSq: '',
            pageIndex: 1,
            pageSize: 10
        }
        //查询
        $scope.search = function () {
            $scope.graybtn = true;
            $scope.requestArr.pageIndex = 1 //查询后在第一页显示
            $scope.ProductName = $scope.requestArr.ProductName;//model重新赋值 传入服务器
            $scope.ProductCode = $scope.requestArr.ProductCode;
            $scope.ProductGroup = $scope.requestArr.ProductGroup;
            $scope.UserId = $scope.requestArr.UserId;
            $scope.ProductStatus = $scope.requestArr.ProductStatus;
            $scope.IsSq = $scope.requestArr.IsSq;
            if ($scope.requestArr.FullstartTime == undefined || $scope.requestArr.FullstartTime == "") {
                $scope.requestArr.FullstartTime = ''
            } else {
                $scope.requestArr.FullstartTime = commonFactory.DateFormat($scope.requestArr.FullstartTime, 1)
            }
            if ($scope.requestArr.FullendTime == undefined || $scope.requestArr.FullendTime == "") {
                $scope.requestArr.FullendTime = ''
            } else {
                $scope.requestArr.FullendTime = commonFactory.DateFormat($scope.requestArr.FullendTime, 1)
            }
            requestProductList();
        }
        // 删除缓存
        $scope.delCache = function () {
            var uId = localStorage.getItem('u_id');
            $.get(commonFactory.baseurl + "/api/common/delkey?key=5C62B8C7-219D-437E-BFE0-A611DD80A70A|*", { u_id: uId }, function (result) {
            })
            $.get(commonFactory.baseurl + "/api/common/delkey?key=AF269182-6CD2-49A7-955E-D94F27617303", { u_id: uId }, function (result) {
            })
            $.get(commonFactory.baseurl + "/api/common/delkey?key=5F3D615F-9475-46F1-BFD4-AE6FAE8C7FA8|*", { u_id: uId }, function (result) {
            })
            $.get(commonFactory.baseurl + "/api/common/delkey?key=B34C61B0-BDF2-430A-B36A-A9BEA5766082|*", { u_id: uId }, function (result) {
            })
        }
        var requestProductList = function () {
            commonFactory.loading(true)
            commonFactory.ajax("/api/Flexible/QueryFlexibleLoanProductList", $scope.requestArr, function (result) {
                $scope.graybtn = false;
                commonFactory.loading(false)
                if (result.Code == 1) {
                    $scope.QueryLoanProductList = result.RealyList;
                    $scope.TotalLoanPrice = result.TotalLoanPrice;
                    $scope.TotalFullPrice = result.TotalFullPrice;
                    $scope.QueryLoanProductListTotal = result.Total;
                    $scope.page = Math.ceil(result.Total / $scope.requestArr.pageSize);
                    $scope.status = function (sta) {
                        switch (sta) {
                            case 1: return '未发布';
                            case 2: return '已发布';
                            case 3: return '募资中';
                            case 4: return '已满标';
                            case 5: return '还款中';
                            case 6: return '提前还清';
                            case 7: return '已还清';
                            case 8: return '等待推送至银行';
                            case 13: return '流标';
                            case 15: return '草稿';
                            case 16: return '已删除';
                        }
                    }

                } else {
                    toaster.pop('error', '失败', result.ResposeData);
                }
            }, function () {
                $scope.graybtn = false;
                toaster.pop('error', '失败', '后台未返回报错');
            }, "post");
        }
        requestProductList();
        //募资中
        $scope.Raise = function (pId) {
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                controller: ['$scope', '$modalInstance', 'raise', function ($scope, $modalInstance, raise) {
                    $scope.raise = raise;
                    $scope.ok = function () {
                        commonFactory.loading(true)
                        commonFactory.ajax("/api/Flexible/ErpSetMuZi", { '': $scope.raise }, function (result) {
                            commonFactory.loading(false)
                            if (result.Code == 1) {
                                toaster.pop('success', '成功', '募资成功');
                                $modalInstance.close();
                            } else {
                                toaster.pop('error', '失败', decodeURIComponent(result.ResposeData));
                                $modalInstance.close();
                            }
                        }, function () {
                            commonFactory.loading(false)
                            toaster.pop('error', '失败', '后台返回报错');
                        }, "post");
                    }
                    $scope.cancel = function () {
                        $modalInstance.close();
                    };
                }
                ],
                size: 'sm',
                backdrop: false,
                resolve: {
                    raise: function () {
                        return pId;
                    }
                }
            });
            modalInstance.result.then(function () {
                requestProductList();
            }, function () {
            });
        }
        //发布
        $scope.Release = function (pid) {
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                controller: ['$scope', '$modalInstance', 'Release', function ($scope, $modalInstance, Release) {
                    $scope.Release = Release;
                    $scope.ok = function () {
                        commonFactory.loading(true)
                        commonFactory.ajax("/api/Flexible/ReleaseProduct", { '': $scope.Release }, function (result) {
                            commonFactory.loading(false)
                            if (result.Code == 1) {
                                toaster.pop('success', '成功', '发布成功');
                                $modalInstance.close();
                            } else {
                                // console.log(decodeURIComponent(result.ResposeData))
                                toaster.pop('error', '失败', decodeURIComponent(result.ResposeData));
                                $modalInstance.close();
                            }
                        }, function () {
                            commonFactory.loading(false)
                            toaster.pop('error', '失败', '后台未返回报错');
                        }, "post");
                    }
                    $scope.cancel = function () {
                        $modalInstance.close();
                    };
                }
                ],
                size: 'sm',
                backdrop: false,
                resolve: {
                    Release: function () {
                        return pid;
                    }
                }
            });
            modalInstance.result.then(function () {
                requestProductList();
            }, function () {
            });

        }
        // 添加
        $scope.addproductdhuo = function () {
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                controller: 'addproducthuo',
                size: 'lg',
                backdrop: true
            });
            modalInstance.result.then(function () {
                requestProductList();
            }, function () {
            });
        }
        //编辑
        $scope.edit = function (pid) {
            localStorage.setItem("editProductHuoId", pid)
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                controller: 'editproducthuo',
                size: 'lg',
                backdrop: true,
                resolve: {
                    edithuo: function () {
                        return pid;
                    }
                }
            });
            modalInstance.result.then(function () {
                requestProductList();
            }, function () {
            });
        }

        //删除
        $scope.delete = function (obj) {
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                controller: ['$scope', '$modalInstance', 'items', 'toaster', 'commonFactory', function ($scope, $modalInstance, items, toaster, commonFactory) {
                    $scope.items = items;
                    $scope.ok = function () {
                        //删除
                        commonFactory.loading(true)
                        commonFactory.ajax("/api/Flexible/DeleteProduct", { '': $scope.items.Id }, function (result) {
                            commonFactory.loading(false)
                            if (result.Code == 1) {
                                toaster.pop('success', '成功', '删除成功');
                                $modalInstance.close();
                            } else {
                                toaster.pop('error', '失败', decodeURIComponent(result.ResposeData));
                                $modalInstance.close();
                            }
                        }, function () {
                            commonFactory.loading(false)
                            toaster.pop('error', '失败', '后台未返回报错');
                        }, "post");
                    }
                    $scope.cancel = function () {
                        $modalInstance.close('cancel');
                    };
                }],
                size: 'sm',
                backdrop: false,
                resolve: {
                    items: function () {
                        return obj;
                    }
                }
            });
            modalInstance.result.then(function () {
                requestProductList();
            }, function () {
            });
        };

        //一键还款
        $scope.hcash = function (pid) {
            commonFactory.ajax("/api/Flexible/GetLoanContractsByProductId", { '': pid }, function (result) {
                $scope.formdata = result.Result;
                $timeout(function () {
                    var formObject = document.querySelector('#idmyForm')
                    formObject.action = result.Url
                    formObject.submit();
                }, 50)
            }, function () { }, 'post')
        }
        // 是否授权
        $scope.editstatus = function (id, status) {
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                controller: ['$scope', '$modalInstance', 'editstatus', function ($scope, $modalInstance, editstatus) {
                    $scope.editstatusid = id;//弹出框判断
                    $scope.editstatus = editstatus;//绑定状态
                    var off = true;
                    $scope.ok = function () {
                        if (!off) { return false }
                        off = false;
                        commonFactory.ajax("/api/Flexible/EditProductAuthStatus", { '': id }, function (result) {
                            toaster.pop('success', '成功', '修改成功');
                            $modalInstance.close();
                            // if (result.code == 1) {

                            // } else {
                            //     toaster.pop('error', '', '失败');
                            //     $modalInstance.close();
                            // }
                        }, function () {
                            toaster.pop('error', '失败', '后台未返回报错');
                        }, "post");
                    }
                    $scope.cancel = function () {
                        $modalInstance.close();
                    };
                }
                ],
                size: 'sm',
                backdrop: false,
                resolve: {
                    editstatus: function () {
                        return status;
                    }
                }
            });
            modalInstance.result.then(function () {
                requestProductList();
            }, function () {
            });
        }

        // 手动满标
        $scope.fullScale = function(pid){
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                controller: ['$scope', '$modalInstance', 'pid', function ($scope, $modalInstance, pid) {
                    $scope.fullscaleShow = pid;
                    $scope.ok = function () {
                        commonFactory.loading(true)
                        commonFactory.ajax("/api/Flexible/FullProductBySystem", { '': pid }, function (result) {
                            commonFactory.loading(false)
                            if (result.Code == 1) {
                                toaster.pop('success', '成功', '满标成功');
                                $modalInstance.close();
                            } else {
                                toaster.pop('error', '失败', decodeURIComponent(result.ResposeData));
                                $modalInstance.close();
                            }
                        }, function () {
                            toaster.pop('error', '失败', '后台未返回报错');
                        }, "post");
                    }
                    $scope.cancel = function () {
                        $modalInstance.close();
                    };
                }
                ],
                size: 'sm',
                backdrop: false,
                resolve: {
                    pid: function () {
                        return pid;
                    }
                }
            });
            modalInstance.result.then(function () {
                requestProductList();
            }, function () {
            });
            // commonFactory.ajax("/api/Flexible/FullProductBySystem", { '': pid }, function (result) {
            //     $scope.formdata = result.Result;
            //     $timeout(function () {
            //         var formObject = document.querySelector('#idmyForm')
            //         formObject.action = result.Url
            //         formObject.submit();
            //     }, 50)
            // }, function () { }, 'post')
        }
        // 虚拟人买标
        $scope.hqmanb = function (obj) {
            $scope.manbitem = {
                canbuyPrice: '',
                id: obj.Id
            }
            var modalInstance = $modal.open({
                templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
                controller: ['$scope', '$modalInstance', 'manbitem', function ($scope, $modalInstance, manbitem) {
                    $scope.hqmanbShow = true
                    $scope.manbitem = manbitem
                    $scope.manbData = {
                        ProductId: manbitem.id,
                        Price: ""
                    }
                    var checkprice = function () {
                        if ($scope.manbData.Price < 500) {
                            toaster.pop('error', '提示', '满标金额必须大于500');
                            return false
                        }
                        if ($scope.manbData.Price == "") {
                            toaster.pop('error', '提示', '请输入满标金额');
                            return false
                        }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
                        return true;
                    }
                    $scope.ok = function () {
                        //满标
                        if (checkprice()) {
                            $('.diabledBtn').attr("disabled", "disabled");
                            commonFactory.loading(true)
                            commonFactory.ajax("/api/Flexible/VirtualBuyFlexible", $scope.manbData, function (result) {
                                commonFactory.loading(false)
                                $('.diabledBtn').removeAttr("disabled");
                                if (result.Code == 1) {
                                    toaster.pop('success', '成功', '满标成功');
                                    $modalInstance.close();
                                } else {
                                    toaster.pop('error', '失败', decodeURIComponent(result.ResposeData));
                                    $modalInstance.close();
                                }
                            }, function () {
                                commonFactory.loading(false)
                                $('.diabledBtn').removeAttr("disabled");
                                toaster.pop('error', '失败', '后台未返回报错');
                            }, "post");
                        }

                    }
                    $scope.cancel = function () {
                        $modalInstance.close();
                    };
                }
                ],
                size: 'sm',
                backdrop: false,
                resolve: {
                    manbitem: function () {
                        return $scope.manbitem;
                    }
                }
            });
            modalInstance.result.then(function () {
                requestProductList();
            }, function () {
            });
        };
    }

    initdata();
}]);
// // 活期添加产品
app.controller('addproducthuo', ['$scope', '$http', 'commonFactory', 'toaster', '$timeout', '$modalInstance', function ($scope, $http, commonFactory, toaster, $timeout, $modalInstance) {
    $scope.addProducthuo = true//判断弹出框显示内容
    $scope.arr = [];
    $scope.allcurrentData = {
        ProductType:'02',
        ProductCode: "",
        ProductName: "",
        UserId: "",
        LoanPrice: "",
        ContractNo: "",
        AnnualRateOfYear: "",
        LoanPeriod: "",
        LoanPeriodUnitId: "1",
        ProductStatus: "",
        // BusinessTypeId: "1",
        BidType:"03",
        PublishTime: "",
        FirstOnlineTime: "",
        ExpireTime: "2020-01-01",
        RepayDate: "2020-01-02",
        FullTime: "",
        ProductSortId: "",
        // IsDeleted
        PropertyList: [],
        ShowRate: "",
        Subscript: "0",
        MinInvestPrice: "",
        MaxInvestPrice: "",
        CountDown: 0,
        Description: "",
        BorrPurpose:"",
        LoanBrower:""
    }
    $scope.checkRate=function(){//限制输入利率范围
        if($scope.allcurrentData.AnnualRateOfYear<=7||$scope.allcurrentData.AnnualRateOfYear>=20){
            $scope.text='输入范围有误，请重新输入'
        }else{
            $scope.text=''
        }
    }
    $scope.noclick = true
    $scope.addProducthuo = function () {
        var all = $(".forminfo")
        var number = $(".forminfo[type='number']")
        for (var i = all.length - 1; i >= 0; i--) {
            if ($(all[i]).val() == "? string: ?" || $(all[i]).val() == "") {
                toaster.pop('warning', '', '数据不能为空');
                return;
            }
        }
        if($scope.allcurrentData.AnnualRateOfYear<=7||$scope.allcurrentData.AnnualRateOfYear>=20){
            toaster.pop('warning', '', '利率输入有误');
            return;
        }
        $scope.allcurrentData.BorrPurpose = $scope.allcurrentData.BorrPurpose.trim()
        if($scope.allcurrentData.BorrPurpose.length>150){
            toaster.pop('warning', '', '借款用途字数不得超过150个，当前字数为'+$scope.allcurrentData.BorrPurpose.trim().length);
            return;
        }
        $scope.allcurrentData.ExpireTime = commonFactory.DateFormat($scope.allcurrentData.ExpireTime)
        $scope.allcurrentData.RepayDate = commonFactory.DateFormat($scope.allcurrentData.RepayDate);
        $scope.allcurrentData.Description = encodeURIComponent($scope.allcurrentData.Description)
        $scope.allcurrentData.LoanBrower = encodeURIComponent($scope.allcurrentData.LoanBrower)
        $scope.noclick = false
        commonFactory.loading(true)
        commonFactory.ajax("/api/Flexible/AddFlexibleProduct", $scope.allcurrentData, function (result) {
            $scope.noclick = true
            commonFactory.loading(false)
            if (result.Code == 1) {
                toaster.pop('success', '', '添加成功');
                $modalInstance.close();
            } else {
                toaster.pop('error', '', '添加失败');
            }
        }, function () {
            commonFactory.loading(false)
            toaster.pop('error', '', '后台报错');
        }, "post");
    }
    $scope.mobile = { mobile: '' };
    $scope.getUserId = function () {
        if ($scope.mobile.mobile == "") {
            toaster.pop('warning', '', '查询内容不能为空');
        } else {
            commonFactory.loading(true)
            commonFactory.ajax("/api/Flexible/ErpGetLoanPersonByMobile", { '': $scope.mobile.mobile }, function (result) {
                commonFactory.loading(false)
                if (result == null) {
                    toaster.pop('warning', '', '查无此人');
                } else {
                    $scope.allcurrentData.UserId = result.Id;
                }
            }, function () {
                commonFactory.loading(false)
                toaster.pop('warning', '', '后台报错');
            }, "post");
        }
    }
    $scope.loginevent = function (e) {
        if (e.keyCode == 13) {
            $scope.getUserId();
        }
    }
}]);
// // 活期期编辑产品editproduct
app.controller('editproducthuo', ['$scope', '$http', 'commonFactory', 'toaster', '$timeout', 'edithuo', '$modalInstance', function ($scope, $http, commonFactory, toaster, $timeout, edithuo, $modalInstance) {
    $scope.productIdhuo = edithuo;
    $scope.alleditData = {
        UserId: ''
    }
    // 通过id 获取 要编辑产品数据
    commonFactory.ajax("/api/Flexible/GetFlexibleProductModelByID", { '': $scope.productIdhuo }, function (result) {
        $scope.alleditData = result;
        if (result == null) {
            toaster.pop('warning', '', '未查询到此Id');
        }
    }, function () {
        toaster.pop('error', '', '后台报错');
    }, "post");
    $scope.editProducthuo = function () {
        var all = $(".forminfo")
        var number = $(".forminfo[type='number']")
        for (var i = all.length - 1; i >= 0; i--) {
            if ($(all[i]).val() == "? string: ?" || $(all[i]).val() == "? object:null ?" || $(all[i]).val() == "") {
                toaster.pop('warning', '', '数据不能为空');
                return;
            }
        }
        if($scope.alleditData.AnnualRateOfYear<=7||$scope.alleditData.AnnualRateOfYear>=20){
            toaster.pop('warning', '', '利率输入有误');
            return;
        }
        $scope.alleditData.BorrPurpose = $scope.alleditData.BorrPurpose.trim()
        if($scope.alleditData.BorrPurpose.length>150){
            toaster.pop('warning', '', '借款用途字数不得超过150个，当前字数为'+$scope.alleditData.BorrPurpose.trim().length);
            return;
        }
        $scope.alleditData.ExpireTime = commonFactory.DateFormat($scope.alleditData.ExpireTime)
        $scope.alleditData.RepayDate = commonFactory.DateFormat($scope.alleditData.RepayDate);
        if ($scope.alleditData.Description) {
            $scope.alleditData.Description = encodeURIComponent($scope.alleditData.Description)
            $scope.alleditData.LoanBrower = encodeURIComponent($scope.alleditData.LoanBrower)
        }
        if ($scope.borrow) {
            $scope.alleditData.UserId = $scope.borrow;
        }
        commonFactory.loading(true)
        commonFactory.ajax("/api/Flexible/EditFlexibleProduct", $scope.alleditData, function (result) {
            commonFactory.loading(false)
            if (result.Code == 1) {
                localStorage.getItem("editProductHuoId", "");
                toaster.pop('success', '', '编辑成功');
                $modalInstance.close();
            } else {
                toaster.pop('warning', '', decodeURIComponent(result.ResposeData));
            }
        }, function () {
            commonFactory.loading(false)
            toaster.pop('error', '', '后台报错');
        }, "post");
    }

    $scope.mobile = { mobile: '' };
    $scope.getUserId = function () {
        if ($scope.mobile.mobile == "") {
            toaster.pop('warning', '', '查询内容不能为空');
        } else {
            commonFactory.loading(true)
            commonFactory.ajax("/api/product/ErpGetLoanPersonByMobile", { '': $scope.mobile.mobile }, function (result) {
                commonFactory.loading(false)
                if (result == null) {
                    toaster.pop('warning', '', '查无此人');
                } else {
                    if (!$scope.allData) {
                        $scope.borrow = result.Id;
                    } else {
                        $scope.allData.UserId = result.Id;
                    }
                }
            }, function () {
                commonFactory.loading(false)
            }, "post");
        }
    }
    $scope.checkRate=function(){//限制输入利率范围
        if($scope.alleditData.AnnualRateOfYear<=7||$scope.alleditData.AnnualRateOfYear>=20){
            $scope.text='输入范围有误，请重新输入'
        }else{
            $scope.text=''
        }
    }
    $scope.loginevent = function (e) {
        if (e.keyCode == 13) {
            e.preventDefault();
            e.stopPropagation();
            $scope.getUserId();
        }
    }
}]);
//订单详情查询
app.controller('queryorder', ['$scope', '$http', 'commonFactory', 'toaster', '$timeout', '$interval', '$modal', function ($scope, $http, commonFactory, toaster, $timeout, $interval, $modal) {
    var initdata = function () {
        $scope.orderData = {//查询条件
            orderId: '',
            type: '1'
        }
        //查询
        $scope.queryOrder = function () {
            requestProductList();
        }
        var checkout = function(){
            if(!$scope.orderData.orderId){
                toaster.pop('warning', '警告', '请输入查询平台订单号');
                return false;
            }
            if(!$scope.orderData.type){
                toaster.pop('warning', '警告', '请选择产品类型');
                return false;
            }
            return true;
        }
        var requestProductList = function () {
            if(checkout()){
                commonFactory.loading(true)
                commonFactory.ajax("/api/product/GetProductByOrderId", $scope.orderData, function (result) {
                    commonFactory.loading(false)
                    if (result.Code == 1) {
                        $scope.orderList = result.Data;

                    } else {
                        toaster.pop('error', '失败', '查询订单不存在');
                    }
                }, function () {
                    toaster.pop('error', '失败', '后台未返回报错');
                }, "get");
                
            }
        }
    }

    initdata();
}]);